const char *help = "\
progname: fft2D.cc\n\
code2html: This program reads a pgm image and computes its FFT 2D.\n\
version: Torch3 vision2.0, 2005\n\
(c) Ewelle Ewelle Richard (eerichard@ifi.edu.vn)\n";


#include "ImageGray.h"
#include "ImageRgb.h"
#include "DiskXFile.h"
#include "CmdLine.h"
#include "Functions.h"
#include <iostream>
#include <vector>
#include "xtprobeImageDiskXFile.h"

using namespace Torch;


/************* PROGRAMME PRINCIPALE *************/


int main(int argc, char **argv)
{
	char *image_filename,*col;
	bool verbose;
	int seuil1;
	int seuil2;
	int seuil3;
	int fu1;
	int fu2;
	int fu3;

	CmdLine cmd;
	cmd.setBOption("write log", false);
  	cmd.info(help);
  	cmd.addText("\nArguments:");
  	cmd.addSCmdArg("image filename", &image_filename, "image filename");
  	cmd.addText("\nOptions:");
 	cmd.addBCmdOption("-verbose", &verbose, false, "verbose");
	cmd.addSCmdOption("-col", &col, "gray", "La camposante de couleur pour les images R G B");
   	cmd.addICmdOption("-s1", &seuil1, 500, "Seuil pour la segmentation: Variance des pixels");
   	cmd.addICmdOption("-s2", &seuil2, -1, "Seuil pour la segmentation: Max-Min");
   	cmd.addICmdOption("-s3", &seuil3, -1, "Seuil pour la segmentation: Autre méthodes");

	cmd.addICmdOption("-f1", &fu1, 100, "Seuil pout la fusion: Variance des pixels");
	cmd.addICmdOption("-f2", &fu2, -1, "Seuil pout la fusion: Max-Min");
	cmd.addICmdOption("-f3", &fu3, -1, "Seuil pout la fusion: Moyennes");
 	cmd.read(argc, argv);

	Image *source = NULL;

        source=loadImage(verbose,image_filename);

	char *name = new char[300];
        char thresh [50];
        int n;
	int seuil;
	int fu;
	char type1[50];
	char type2[50];
	char title[50];

//**	Identification du critère de segmentation

	if((seuil2==-1)&&(seuil3==-1)){
		seuil=seuil1;
		strcpy(type1,"variance");
        	strcpy(title,"_VA");
	}
	else{
		if(seuil3==-1){
			seuil=seuil2;
			strcpy(type1,"maxmin");
			strcpy(title,"_MM");
		}
		else{
			seuil=seuil3;
			strcpy(type1,"moyenne");
			strcpy(title,"_MOY");
		}		
	}

//**	Identification du critère de fusion

	if((fu2==-1)&&(fu3==-1)){
		fu=fu1;
		strcpy(type2,"variance");
		strcat(title,"-VA(");
	}
	else{
		if(fu3==-1){
			fu=fu2;
			strcpy(type2,"maxmin");
			strcat(title,"-MM(");
		}
		else{
			fu=fu3;
			strcpy(type2,"moyenne");
			strcat(title,"-MOY(");
		}		
	}


	n=sprintf (thresh, "%d - %d)-", seuil,fu);
        strcpy(name, setTitle(image_filename,title));
        strcat(name, thresh);
	strcat(name, col);
        strcat(name, (char*)".tiff");
	
    	Functions TheImage(source,seuil,fu,col);

	TheImage.Split(type1);        	
        TheImage.Merge(type2);


        Image * OutImage;
	OutImage = new ImageRgb(source->width,source->height);
	TheImage.Afficher_RGB(OutImage); 
  	
	saveImage(OutImage, name);
        
	delete source;
	delete OutImage;

	return(0);
}
